﻿@model Prime.Models.Lesson

<script type="text/javascript">
    function HomeFieldCtrl($scope, $http) {

        $scope.array = [];
        $scope.done = 0;
        $scope.log = function (message) {
            $http.get('/api/Log?message='+message).success(function (data) {
                $scope.message = "Log successful"
            }).error(function (data, status, header, config) {
                $scope.message="Log error!";
            });
        }
        $scope.log("Lekcija zapoceta od strane korisnika!");
        var uri = "/api/Lesson/@ViewBag.LessonId";
        $http.get(uri).success(function (data) {
            $scope.data = data;
            $scope.mySort = function (one) {
                return one.PartNumber;
            };

            $scope.data.Materials.forEach(function (entry) {
                $scope.array[entry.Data.PartNumber-1] = entry;
            });

            $scope.data.Questions.forEach(function (entry) {
                $scope.array[entry.Data.PartNumber - 1] = entry;
                entry.questions = entry.Data.Answers.split("@@").filter(function (el) { return el.length != 0 });;
            });
            $scope.done = 1;
            
        }).error(function(header, status, data, config){
            window.alert("Greska u konekciji sa serverom");
        });

        //defining curr material
        $scope.curr_pos = 0;
        
        $scope.lives = 5;
        

        //skiping material
        $scope.SkipMaterial = function () {
            $scope.curr_pos++;
        }

        $scope.CheckAnswer = function (item) {
            if (item == $scope.array[$scope.curr_pos].Data.TrueAnswer) {
                $scope.curr_pos++;
                $scope.log("Correct. PK ("+ $scope.array[$scope.curr_pos].Data.LessonId +","+ $scope.array[$scope.curr_pos].Data.PartNumber+")");
            }
            else {
                $scope.lives--;
                $scope.log("Wrong. PK (" + $scope.array[$scope.curr_pos].Data.LessonId + "," + $scope.array[$scope.curr_pos].Data.PartNumber + ")");
                if ($scope.lives == 0) {
                    window.alert("NEMA ZIVOTA!");
                }
            }
        }

        

    }
</script>

<div ng-controller="HomeFieldCtrl">
    <button ng-click="log('Neka poruka jbt!')"> test </button>
    {{array[curr_pos]}}
    <br />
    {{lives}}
    <br />
    <div ng-if="array[curr_pos].type == 'Material'">
        {{array[curr_pos].Data.MaterialPath}}
        <br />
        {{array[curr_pos].Data.MaterialType}}
        <br />
        <button ng-click="SkipMaterial()"> Skip </button>
    </div>
    <div ng-if="array[curr_pos].type=='Question' ">
        {{array[curr_pos].Data.Question}}
        <br />
        <button ng-repeat="item in array[curr_pos].questions" ng-click="CheckAnswer(item)"> {{item}} </button>
    </div>

</div>
